package shop.controller.front.user;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wzl on 2017/9/21.
 */
@Controller
@RequestMapping("/front/user/")
public class UserController extends FrontBaseController<User> {
    private final static String USER_ROLE_MANAGE_ADMIN = "1";
    private final static String USER_ROLE_FRONT_COMMON = "2";
    @Resource
    private UserService userService;

    @Override
    public Services<User> getService() {
        return userService;
    }


    /**
     * 跳转到微信首页
     *
     * @return
     */
    @RequestMapping("toIndex")
    public String toIndex() {
        return "/front/index";
    }

    /**
     * 跳转到个人中心
     *
     * @return
     */
    @RequestMapping("toMyPage")
    public String toMyPage() {
        return "/front/user/userList";
    }

    /**
     * 跳转到个人信息页面
     *
     * @return
     */
    @RequestMapping("toPersonal")
    public String toPersonal() {
        return "/front/user/userCenter";
    }


    /**
     * 跳转到选择购物券
     *
     * @return
     */
    @RequestMapping("selectVoucherList")
    public String toStoreList() {
        return "/front/user/selectVoucher";
    }

    /**
     * 跳转到我的团队页面
     *
     * @return
     */
    @RequestMapping("toMyTeam")
    public String toMyTeam() {
        return "/front/user/myTeam";
    }

    /**
     * 跳转到关于我们
     *
     * @return
     */
    @RequestMapping("toAboutUs")
    public String toAboutUs() {
        return "/front/user/aboutUs";
    }

    /**
     * 跳转到绑定手机
     *
     * @return
     */
    @RequestMapping("toBind")
    public String toSubOrder() {
        return "/front/user/bindPhone";
    }


    /**
     * 获取缓存用户信息
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "getSession", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getSession(HttpSession session) {
        JSONResult jsonResult = new JSONResult();
        Map result = new HashMap<>();
        if (session.getAttribute("userInfo") != null) {
            result.put("user", session.getAttribute("userInfo"));
        } else {
            result.put("user", null);
        }
        jsonResult.setData(result);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 更新缓存用户信息
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "updateSession", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult updateSession(HttpSession session) {
        JSONResult jsonResult = new JSONResult();
        Map result = new HashMap<>();
        if (session.getAttribute("userInfo") != null) {
            User user = (User) session.getAttribute("userInfo");
            user = userService.selectById(user.getId());
            session.setAttribute("userInfo", user);
            result.put("user", session.getAttribute("userInfo"));
        } else {
            result.put("user", null);
        }
        jsonResult.setData(result);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询登录用户
     *
     * @param
     * @return
     */
    @RequestMapping(value = "selectUser", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectUser(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("userInfo");
        jsonResult = new JSONResult();
        jsonResult.setData(user);
        return jsonResult;
    }

    /**
     * 更新用户信息
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "updateUser", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateUser(HttpSession session, User user) throws Exception {
        jsonResult = new JSONResult();
        userService.update(user);
        User userInfo = userService.selectOne(user);
        session.setAttribute("userInfo", userInfo);//更新缓存
        jsonResult.setData(userInfo);
        return jsonResult;
    }
    @Override
    public JSONResult deleteJson(HttpServletRequest request, User user) throws Exception {
        return null;
    }
}
